<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Serenity
 * Date: 28-6-11
 * Time: 8:54
 * To change this template use File | Settings | File Templates.
 */

require_once(Config::acquire('Model', null, 'model'));
require_once(Config::acquire('Software', MODEL));
require_once(Config::acquire('Hardware', MODEL));
class GeinstaleerdeSoftware extends model
{


    private $hardware;
    private $software = array();

    public static function read($id)
    {
        $software = array();
        $list = SQL::query("SELECT * FROM `geinstaleerde_software` WHERE `hardware_ID` = $id");
        while ($list->isValid()) {
            $software[] = Software::read($list->fetchField('software_ID'));
            $list->next();
        }
        return GeinstaleerdeSoftware::create(Hardware::read($id), $software);
    }

    public function __construct(Hardware $hardware, $software)
    {
        $this->hardware = $hardware;
        $this->software = $software;
    }

    public static function create(Hardware $hardware, $sofware)
    {

        return new GeinstaleerdeSoftware($hardware, $sofware);
    }

    public function delete($softwareID = null)
    {
        if (!is_null($softwareID)) {
            SQL::execute("DELETE FROM `geinstaleerde_software` WHERE `hardware_ID` = '" . $this->hardware->getId() . "' AND `software_ID` = '$softwareID'");
        }
    }

    public function add($softwareID)
    {
        SQL::execute("INSERT INTO `geinstaleerde_software` (`hardware_ID`, `software_ID`) VALUES ({$this->hardware->getId()}, $softwareID)");
    }

    public function toArray()
    {

    }

    public function update()
    {
        return;
    }

    public function setSoftware($software)
    {
        $this->software = $software;
    }

    public function getSoftware()
    {
        return $this->software;
    }

    public function getUnique()
    {
        $list = array();
        foreach (Software::readAll() as $program) {
            $list[$program->getID()] = $program->getNaam();
        }
        foreach ($this->software as $program)
            unset($list[$program->getID()]);
        return $list;
    }


}
